﻿using System.Collections;
using System.IO;

using UnityEngine;
using YooAsset;

namespace Framework.Resource
{

    public partial class YooassetResourceHelper
    {
        
         IEnumerator EditorModeInit(InitResourceCallback initResourcesCompleteCallback)
        {
            var initParameters = new EditorSimulateModeParameters();
            var simulateManifestFilePath = EditorSimulateModeHelper.SimulateBuild(EDefaultBuildPipeline.BuiltinBuildPipeline, "DefaultPackage");
            initParameters.SimulateManifestFilePath  = simulateManifestFilePath;
            yield return package.InitializeAsync(initParameters);
            initResourcesCompleteCallback.Invoke(true);
        }

        IEnumerator OfflineModeInit(InitResourceCallback initResourcesCompleteCallback)
        {
            var initParameters = new OfflinePlayModeParameters();
            initParameters.DecryptionServices = new FileOffsetDecryption();
            yield return package.InitializeAsync(initParameters);
            initResourcesCompleteCallback.Invoke(true);
        }

       
        IEnumerator WebPlayModeInit(InitResourceCallback initResourcesCompleteCallback)
        {
            // 注意：GameQueryServices.cs 太空战机的脚本类，详细见StreamingAssetsHelper.cs
            string defaultHostServer = "http://127.0.0.1/CDN/WebGL/v1.0";
            string fallbackHostServer = "http://127.0.0.1/CDN/WebGL/v1.0";
            var initParameters = new WebPlayModeParameters();
            initParameters.BuildinQueryServices = new GameQueryServices();
            initParameters.DecryptionServices = new FileOffsetDecryption();
            initParameters.RemoteServices = new RemoteServices(defaultHostServer, fallbackHostServer);
            var initOperation = package.InitializeAsync(initParameters);
            yield return initOperation;
        
            if(initOperation.Status == EOperationStatus.Succeed)
            {
                Debug.Log("资源包初始化成功！");
                initResourcesCompleteCallback.Invoke(true);
            }
            else 
            {
                initResourcesCompleteCallback.Invoke(false);
                Debug.LogError($"资源包初始化失败：{initOperation.Error}");
            }
        }
        IEnumerator HostPlayModeModeInit(InitResourceCallback initResourcesCompleteCallback)
        {
            // 注意：GameQueryServices.cs 太空战机的脚本类，详细见StreamingAssetsHelper.cs
            string defaultHostServer = "http://cnd.zweiluan.top/new_game/v1.0";
            string fallbackHostServer = "http://cnd.zweiluan.top/new_game/v1.0";
            var initParameters = new HostPlayModeParameters();
            initParameters.BuildinQueryServices = new GameQueryServices(); 
            initParameters.DecryptionServices = new FileOffsetDecryption();
            initParameters.RemoteServices = new RemoteServices(defaultHostServer, fallbackHostServer);
            var initOperation = package.InitializeAsync(initParameters);
            yield return initOperation;
        
            if(initOperation.Status == EOperationStatus.Succeed)
            {
                Debug.Log("资源包初始化成功！");
                initResourcesCompleteCallback.Invoke(true);
            }
            else 
            {
                initResourcesCompleteCallback.Invoke(false);
                Debug.LogError($"资源包初始化失败：{initOperation.Error}");
            }
        }
        
    }





}